package indus.icalendarFileGenerator;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

/**
 *  The main class that will call the Event class in order
 *  to get input from a user and write into a .ics file
 *
 *  @author Team Indus
 *  @date August 13th 2014
 *  @bugs None
 */
public class IcsFileGeneratorDriver {
        
    public static void main(String[] args) {
    	
    		int debugmode = -1;
    		int selection = -1;
    		Scanner scan = new Scanner(System.in);

    		if(args.length != 0 && args[0].equals("debug"))
    		{
    		    debugmode = 1;
    		    System.out.println("*****DEBUGMODE*****");
    		    System.out.println("All method calls will be printed\n");
    		}
    		
			  System.out.println("Welcome to the ics file generator driver program!");
    		
    		//Main menu loop
    		while(selection != 3) {
    			  System.out.println("Please select an option below: ");
    		  	System.out.println("(1) Write an ics file");
    			  System.out.println("(2) Information");
    		  	System.out.println("(3) Quit");
    		  	
    		  	try {
    		  		if(debugmode == 1) {
    		  			System.out.println("Try: Scanning for next int: ");
    		  		}
    		      selection = scan.nextInt();
    		  	}
    		  	catch(Exception e) {
    		  		if(scan.hasNextLine()) {
    		  			if(debugmode == 1) {
      		  		  System.out.println("Catch: scan.hasNextLine()");
      		  		}
      		  	  scan.nextLine();
    		  		}
    		  	}
    			
            if(selection == 1) {
    	        if(debugmode == 1) {
    			      System.out.println("Calling constructor for a new Event...");
    	  	    }
    		
              Event event = new Event();
        
              if(debugmode == 1) {
                System.out.println("New event object created...\n");
                System.out.println("Calling event.setVersion()...");
              }
        
              event.setVersion();
        
              if(debugmode == 1 ) {
              	System.out.println("End of setVersion()...\n");
              	System.out.println("Calling event.setStatus()...");
              }
            
              event.setStatus();
        
              if(debugmode == 1) {
                System.out.println("End of setStatus()...\n");
                System.out.println("Calling event.setSummary()...");
              }
              
              event.setSummary();
        
              if(debugmode == 1) {
                System.out.println("End of setSummary()...\n");
                System.out.println("Calling event.setLocation()...");
              }
        
              event.setLocation();
        
              if(debugmode == 1) {
                System.out.println("End of setLocation()...\n");
                System.out.println("Calling event.setTimeZoneIdentifier()...");
              }
        
              event.setTimeZoneIdentifier();
        
              if(debugmode == 1) {
                System.out.println("End of setTimeZoneIdentifier()...\n");
                System.out.println("Calling event.setStartDate()...");
              }
        
              event.setStartDate();
              
              if(debugmode == 1) {
                System.out.println("End of setStartDate()...\n");
                System.out.println("Calling event.setEndDate()...");
              }
           
              event.setEndDate();
              
              if(debugmode == 1) {
                System.out.println("End of setEndDate()...\n");
                System.out.println("Calling event.setStartTime...");
              }
              
              event.setStartTime();
              
              if(debugmode == 1) {
              	System.out.println("End of setStartTime()...\n");
              	System.out.println("Calling event.setEndTime()...");
              }
              
              event.setEndTime();
              
              if(debugmode == 1) {
              	System.out.println("End of setEndTime...\n");
              	System.out.println("Calling event.event.setReoccurenceText...");
              }
              
              event.setReoccurrenceText();
              
              if(debugmode == 1) {
                	System.out.println("End of setReoccurenceText...\n");
                	System.out.println("Calling event.setClassification...");
                }
              
              event.setClassification();
        
              if(debugmode == 1) {
                System.out.println("End of setClassification()...\n");
                System.out.println("Calling event.setGEO()...");
              }
        
              event.setGEO();
        
              if(debugmode == 1) {
                System.out.println("End of setGEO()...\n");
                System.out.println("Calling event.setPriorityValue()...");
              }
        
              event.setPriorityValue();
        
              if(debugmode == 1) {
              	System.out.println("End of setPriorityValue()...\n");
            	  System.out.println("Creating text for ics file...");
              }
		
              String icsFileText;
        
              icsFileText = "BEGIN:VCALENDAR\n" +
       	      "VERSION:" + event.getVersion() + "\n" +
              "STATUS:" + event.getStatus() + "\n" +
              "X-WR-TIMEZONE:" + event.getTimeZoneIdentifier() + "\n" +
              "BEGIN:VEVENT\n" +
              "DTSTART;TZID="+event.getTimeZoneIdentifier()+":"+event.getStartDate()+ "T" + event.getStartTime() +"00\n"+
              "DTEND;TZID="+event.getTimeZoneIdentifier()+":"+event.getEndDate()+ "T" + event.getEndTime() +"00\n"+
              "SUMMARY:" + event.getSummary() + "\n" + 
              "LOCATION:" + event.getLocation() + "\n" + event.getReoccurrenceText() +
              "CLASS:" + event.getClassification() + "\n" +
              "GEO:" + event.getGEO() + "\n" +
              "PRIORITY:" + event.getPriorityValue() + "\n" +
              "END:VEVENT\n" +
              "END:VCALENDAR\n";

              if(debugmode == 1) {
        	      System.out.println("Text to be written into ics file: \n");
        	      System.out.println(icsFileText);
              }
        
              try {
              	  System.out.println("Writing into .ics file");
                  BufferedWriter writer = null;
                  writer = new BufferedWriter(new FileWriter("event.ics"));
                  writer.write(icsFileText);
                  writer.close();
                  System.out.println("File has been successfully written\n");
              }
              catch(IOException e) {
                  System.out.println("Error: Could not write .ics file");
                  System.out.println(e);
              }
              //To clear scanner when it goes back to the menu
              selection = -1;
            } //End of selection 1
            else if(selection == 2) {
            	System.out.println("Author: Team Indus");
            	System.out.println("This program is compliant with RFC 5545");
            	System.out.println("For source code, please go to https://code.google.com/p/indus-icalendar/\n");
            	selection = -1;
            } //End of selection 2
            else if(selection == 3) {
              System.out.println("Exiting program, thank you");
            } //End of selection 3
          
    		}//End of main menu while loop
    scan.close();
    }//End of main
		
}//End of IcsFileGeneratorDriver class